package com.winchain.chainbill.bill.biz.repurchase.cmd;

import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.framework.command.CallbackContext;
import com.shell.framework.command.CommandContext;
import com.shell.framework.command.ICallback;
import com.shell.framework.command.ICommand;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.base.po.CbRpur;
import com.winchain.chainbill.bill.base.service.CbDctService;
import com.winchain.chainbill.bill.base.service.CbInsAcrService;
import com.winchain.chainbill.bill.biz.Bill;
import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillDctRpurCmd implements ICommand<Bill<CbRpur>>, ICallback<Bill<CbRpur>, AcctTxRec> {
	@Autowired
	private CbInsAcrService cbInsAcrService;
	@Autowired
	private CbDctService cbDctService;

	public boolean execute(CommandContext<Bill<CbRpur>> context) throws Exception {
		CbRpur cbRpur = (CbRpur) ((Bill) context.getSource()).getSource();
		if (cbRpur != null) {
			String dctId = cbRpur.getDctId();
			int delResult = this.cbInsAcrService.deleteCbInsAcrByBizId(dctId);
			if (delResult > 0) {
				CbDct cbDct = (CbDct) this.cbDctService.selectByKey(dctId);
				cbDct.setExpDate(cbRpur.getRpurDate());
				cbDct.setFixedRate(cbDct.getFixedRate().subtract(cbRpur.getFixedRate()));
				cbDct.setVarRate(cbDct.getVarRate().subtract(cbRpur.getVarRate()));
				this.cbDctService.saveBillDctsByDctDate(cbDct, cbRpur.getRpurInt(), true);
			}
		}
		return false;
	}

	public boolean callback(CallbackContext<Bill<CbRpur>, AcctTxRec> context) throws Exception {
		execute(context);
		return false;
	}
}
